package CelestialDataAccess;

import javax.sql.rowset.*; //provides cached row set interface
import java.util.Date;
import java.util.Calendar;

public final class Customer extends User implements Comparable, IKey<Integer>{

    //<editor-fold defaultstate="collapsed" desc="attributes.">
    private int m_iCustomerID = -1;
    private Date m_dtRegisteredOn = Calendar.getInstance().getTime();

    private String m_strContact = "";
    private Date m_dtDOB = Calendar.getInstance().getTime();;
    private String m_strCountry = "";
    private String m_strCity = "";
    private String m_strState = "";
    private String m_strAddressLine1 = "";
    private String m_strAddressLine2 = "";
    private String m_strPostalCode = "";
    
    private String m_strCreditCardNumber = "";
    private String m_strCreditCardCVV = "";
    private String m_strCreditCardHolder = "";
    private int m_iCreditCardType = 1;
    private Date m_dtCreditCardExpiry = Calendar.getInstance().getTime(); ;

    private boolean m_bIsActive = true;

    private static IFactory<Customer> m_factory = new IFactory<Customer>(){
        public Customer createT(CachedRowSet crs){

            Customer cust;
            try{
                if(crs.getRow() < 1) return null;

                cust = new Customer();

                cust.m_iCustomerID = crs.getInt("CustomerID");
                cust.m_strPassword = CryptService.decrypt(crs.getBytes("Password"));
                cust.m_iUserLevel = crs.getInt("UserLevel");
                cust.m_dtRegisteredOn = crs.getDate("RegisteredOn");

                cust.m_strFirstName = crs.getString("FirstName");
                cust.m_strLastName = crs.getString("LastName");
                cust.m_strEmail = crs.getString("Email");
                cust.m_strContact = crs.getString("Contact");
                cust.m_dtDOB = crs.getDate("DOB");

                cust.m_strCountry = crs.getString("Country");
                cust.m_strCity = crs.getString("City");
                cust.m_strState = crs.getString("State");
                cust.m_strAddressLine1 = crs.getString("AddressLine1");
                cust.m_strAddressLine2 = crs.getString("AddressLine2");
                cust.m_strPostalCode = crs.getString("PostalCode");

                cust.m_strCreditCardNumber = crs.getString("CreditCardNumber");
                cust.m_strCreditCardCVV = crs.getString("CreditCardCVV");
                cust.m_strCreditCardHolder = crs.getString("CreditCardHolder");
                cust.m_dtCreditCardExpiry  =crs.getDate("CreditCardExpiry");
                cust.m_iCreditCardType = crs.getInt("CreditCardType");

                cust.m_bIsActive  =crs.getBoolean("IsActive");
        }
        catch(Exception ex){ //TODO: define custom ObjectCreationException
            return null;
        }
        return cust;
        }
    };// </editor-fold>
    /*************************************************************************/
    //<editor-fold defaultstate="collapsed" desc="get/set methods.">
    public int getCustomerID(){ return m_iCustomerID;}
    /*-----------------------------------------------------------------------*/
    public Date getRegistrationDate(){return m_dtRegisteredOn;}
    public void setRegistrationDate(Date rdate){ m_dtRegisteredOn = rdate; }

    /*-----------------------------------------------------------------------*/
    public String getContact(){return m_strContact;}
    public void setContact(String contact){ m_strContact = contact; }
    /*-----------------------------------------------------------------------*/
     public Date getDOB(){return m_dtDOB;}
    public void setDOB(Date dob){ m_dtDOB = dob; }
    /*-----------------------------------------------------------------------*/
    public String getCountry(){return m_strCountry;}
    public void setCountry(String country){ m_strCountry = country; }
    /*-----------------------------------------------------------------------*/
    public String getCity(){return m_strCity;}
    public void setCity(String city){ m_strCity = city; }
    /*-----------------------------------------------------------------------*/
    public String getState(){return m_strState;}
    public void setState(String state){ m_strState = state; }
    /*-----------------------------------------------------------------------*/
    public String getAddressLine1(){return m_strAddressLine1;}
    public void setAddressLine1(String addr1){ m_strAddressLine1 = addr1; }
    /*-----------------------------------------------------------------------*/
    public String getAddressLine2(){return m_strAddressLine2;}
    public void setAddressLine2(String addr2){ m_strAddressLine2 = addr2; }
    /*-----------------------------------------------------------------------*/
    public String getPostalCode(){return m_strPostalCode;}
    public void setPostalCode(String pcode){ m_strPostalCode = pcode; }

    /*-----------------------------------------------------------------------*/
    public String getCreditCardNumber(){return m_strCreditCardNumber;}
    public void setCreditCardNumber(String ccnum){ m_strCreditCardNumber = ccnum; }
    /*-----------------------------------------------------------------------*/
    public String getCreditCardCVV(){return m_strCreditCardCVV;}
    public void setCreditCardCVV(String cvv){ m_strCreditCardCVV = cvv; }
    /*-----------------------------------------------------------------------*/
    public String getCreditCardHolder(){return m_strCreditCardHolder;}
    public void setCreditCardHolder(String holder){ m_strCreditCardHolder = holder; }
    /*-----------------------------------------------------------------------*/
    public int getCreditCardType(){return m_iCreditCardType;}
    public void setCreditCardType(int type){ m_iCreditCardType = type; }
    /*-----------------------------------------------------------------------*/
    public Date getCreditCardExpiry(){return m_dtCreditCardExpiry;}
    public void setCreditCardExpiry(Date expdate){ m_dtCreditCardExpiry = expdate; }
    /*-----------------------------------------------------------------------*/

    public boolean isActive(){return m_bIsActive;}
    public void setActive(Boolean active){ m_bIsActive = active; }

    /*-----------------------------------------------------------------------*/
    static IFactory getFactory(){ return m_factory; } // </editor-fold>
    /*************************************************************************/
    //<editor-fold defaultstate="collapsed" desc="constructors">
    public Customer(){
        m_iCustomerID = 0;
        m_dtRegisteredOn = new Date();
        m_strPassword = "12345678";
    }
    /*-----------------------------------------------------------------------*/
    /* factory constructor */
    static Customer createCustomer(CachedRowSet crs){
        return m_factory.createT(crs);
    } //</editor-fold>
    /*************************************************************************/
    //<editor-fold defaultstate="collapsed" desc="interfaces">
    /* IKey */
    public Integer getKey(){ return m_iCustomerID; }
    /*************************************************************************/
    /* IComparable */
    public int compareTo(Object cust){

        if(this.m_iCustomerID==((Customer)cust).m_iCustomerID)
            return 0;
        else if(this.m_iCustomerID > ((Customer)cust).m_iCustomerID)
            return 1;
        else
            return -1;
    } //</editor-fold>
    /*************************************************************************/
    public String toString(boolean details){

        if(details)
            return m_iCustomerID + ";   " + m_strPassword + ";   " + m_strFirstName + ";   " +
                    m_strLastName + ";   " + m_dtRegisteredOn + ";   " + m_dtDOB +
                    m_strEmail + ";   " + m_strContact + ";   " +
                    m_strCountry + ";   " + m_strCity + ";   " +
                    m_strState + ";   " + m_strAddressLine1 + ";   " +
                    m_strAddressLine2 + ";   " + m_strPostalCode + ";   " +
                    m_strCreditCardNumber + ";   " + m_strCreditCardCVV + ";   " +
                    m_iCreditCardType + ";   " +
                    m_dtCreditCardExpiry + ";   " + m_strCreditCardHolder + ";   ";
        else
            return toString();
    }

    public @Override String toString(){ return getName(); }

    public @Override Customer clone(){
        Customer c = new Customer();   
        return c;
    }
}
